<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                新增下级菜单
            </el-header>
            <el-main>
                <el-form ref="addMenuData" :rules="rules" :model="addMenuData" label-width="115px"
                         label-position="left">
                    <el-form-item label="菜单名称 :" prop="name">
                        <el-input v-model="addMenuData.name" placeholder="请输入菜单名称"></el-input>
                    </el-form-item>
                    <el-form-item label="菜单别名 :" prop="alias">
                        <el-input v-model="addMenuData.alias" placeholder="请输入菜单别名"></el-input>
                    </el-form-item>
                    <el-form-item label="菜单URL地址 :" prop="url">
                        <el-input v-model="addMenuData.url" placeholder="请输入菜单URL地址"></el-input>
                    </el-form-item>
                    <el-form-item label="菜单图标地址 :">
                        <el-input v-model="addMenuData.ico" placeholder="请输入菜单图标地址"></el-input>
                    </el-form-item>
                    <el-form-item label="菜单排序序号 :" prop="inx">
                        <el-input v-model="addMenuData.inx" placeholder="请输入菜单排序序号"></el-input>
                    </el-form-item>
                    <el-form-item label="菜单是否启用 :" prop="enabled">
                        <el-radio-group v-model="addMenuData.enabled">
                            <el-radio :label="true">启用</el-radio>
                            <el-radio :label="false">不启用</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="菜单是否可见 :" prop="visible">
                        <el-radio-group v-model="addMenuData.visible">
                            <el-radio :label="true">可见</el-radio>
                            <el-radio :label="false">不可见</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="父级名称">
                        <el-row>{{addMenuData.patName}}</el-row>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        完成新增
                    </el-button>
                    <el-button @click="editOut">
                        取消新增
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {replaceSpacesObj} from '../../../utils/webUtils.js';

    export default {
        name: "addMenu",
        data() {
            return {
                formAlignLeft: 'left',
                uuid: this.menuUuid,
                addMenuData: {
                    name: '',
                    url: '',
                    alias: '',
                    ico: '',
                    inx: '5',
                    patUuid: '',
                    patName: '',
                    enabled: '',
                    visible: ''
                },
                rules: {
                    name: [
                        {required: true, message: '菜单名称不得为空', trigger: 'blur'},
                    ],
                    url: [
                        {required: true, message: 'URL地址不得为空', trigger: 'blur'},
                    ],
                    alias: [
                        {required: true, message: '菜单别名不得为空', trigger: 'blur'},
                    ],
                    inx: [
                        {required: true, message: '菜单排序不得为空', trigger: 'blur'},
                    ],
                    enabled: [
                        {required: true, message: '此选项为必选选项', trigger: 'blur'},
                    ],
                    visible: [
                        {required: true, message: '此选项为必选选项', trigger: 'blur'},
                    ]
                },
                info: '',
                infoShow: false
            }
        },
        props: {
            menuUuid: {
                type: String
            }
        },
        //深度监听父组件的props传值
        watch: {
            menuUuid(val) {
                this.uuid = val;
                this.queryMenu();
            },
            other: {
                //深度观察对象
                handler(val) {
                },
                deep: true
            }
        },
        mounted() {
            this.queryMenu();
        },
        methods: {
            queryMenu() {
                this.axios.request({
                    method: "get",
                    url: "./kwp/kdi/menu/" + this.uuid,
                    headers: {},
                    params: {}
                }).then(response => {
                    this.addMenuData.patUuid = response.data.uuid;
                    this.addMenuData.patName = response.data.name;
                }).catch(error => {
                    console.log(error);
                });
            },
            submit() {
                this.$refs['addMenuData'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.axios.request({
                        method: "post",
                        url: "./kwp/kdi/menu",
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8"
                        },
                        data: replaceSpacesObj(this.addMenuData)
                    }).then(response => {
                        this.info = '新增成功,两秒后关闭';
                        this.infoShow = true;
                        setTimeout(() => {
                            this.infoShow = false;
                            this.$emit("editState", false);
                            this.cleanMenu();
                            this.info = '';
                        }, 2000);
                    }).catch(error => {
                        console.log(error);
                        this.info = '新增失敗';
                        this.infoShow = true;
                    });
                });
            },
            editOut() {
                this.infoShow = false;
                this.cleanMenu();
                this.info = '';
                this.$emit("editState", false);
            },
            cleanMenu() {
                this.addMenuData = {
                    name: '',
                    url: '',
                    alias: '',
                    ico: '',
                    inx: '5',
                    patUuid: '',
                    patName: '',
                    enabled: '',
                    visible: ''
                };
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 334px;
        height: 100%;
        z-index: 100;
    }


</style>